package com.intel.factorytest.application;

import java.util.Locale;

import com.intel.factorytest.cfparser.ConfigFileParseUtil;
import com.intel.factorytest.cfparser.concrete.AConfigFileParser;
import com.intel.factorytest.cfparser.concrete.PropertiesConfParser;
import com.intel.factorytest.constants.PropertiesConstants;
import com.intel.factorytest.util.PropertiesUtil;

import android.app.Application;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.util.DisplayMetrics;
import android.util.Log;

public class MATApplication extends Application {

    //private static final String LANGUAGE = "zh";
    private static final String LANGUAGE = "en";

    public static final String LANGUAGE_PREF = "Language";
    public static final String LANGUAGE_KEY = LANGUAGE_PREF;

    public static int mScreenOrientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;// .ORIENTATION_PORTRAIT;
    public static boolean mScreenOrientationDefine = false;
    
    public static int iProductType = 1;

    @Override
    public void onCreate() {
        super.onCreate();

        updateResourcesConfiguration();

        saveLanguage();

        String sdOrientation = ((PropertiesConfParser) ConfigFileParseUtil
                .getConfParserConcrete(this,
                        AConfigFileParser.EXTENSION_PROPERTISE)).getProperties(
                false).getProperty(PropertiesConstants.KEY_ORIENTATION);

        if (sdOrientation == null) {
            mScreenOrientationDefine = false;
        } else {
            if (sdOrientation.equals("SCREEN_ORIENTATION_LANDSCAPE")) {
                mScreenOrientationDefine = true;
                mScreenOrientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
            } else if (sdOrientation.equals("SCREEN_ORIENTATION_PORTRAIT")) {
                mScreenOrientationDefine = true;
                mScreenOrientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
            }
        }
        
        
        String sProductType = ((PropertiesConfParser) ConfigFileParseUtil
                .getConfParserConcrete(this,
                        AConfigFileParser.EXTENSION_PROPERTISE)).getProperties(
                false).getProperty(PropertiesConstants.KEY_PRODUCT, "1");
        
        try {
            iProductType = Integer.valueOf(sProductType);
        } catch (NumberFormatException e) {
            iProductType = PropertiesConstants.VALUE_PRODUCT_SOFIA; // default: [1] SoFIA
        }

        // Log.e("galen", "sdOrientation:"+sdOrientation);
    }

    private void updateResourcesConfiguration() {
        Locale locale = new Locale(LANGUAGE);
        Locale.setDefault(locale);
        Configuration config = getResources().getConfiguration();
        DisplayMetrics metrics = getResources().getDisplayMetrics();
        if (LANGUAGE.equals("zh")) {
            config.locale = Locale.SIMPLIFIED_CHINESE;
        } else {
            config.locale = Locale.ENGLISH;
        }
        getResources().updateConfiguration(config, metrics);
    }

    private void saveLanguage() {
        SharedPreferences pre = getSharedPreferences(LANGUAGE_PREF,
                Context.MODE_PRIVATE);
        //String mLocalLangage = Locale.getDefault().getLanguage();
        Editor editor = pre.edit();
        editor.putString(LANGUAGE_KEY, LANGUAGE);
        //editor.putString(LANGUAGE_KEY, mLocalLangage);
        editor.commit();
    }

    public int getScreenOrientation() {
        Log.e("galen", "mScreenOrientation:" + mScreenOrientation);
        return mScreenOrientation;
    }

    public boolean getScreenOrientationDefine() {
        Log.e("galen", "mScreenOrientationDefine:" + mScreenOrientationDefine);
        return mScreenOrientationDefine;
    }

}
